package com.lp.component_master;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentContainerView;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.lp.module_lib.api.BasePresenter;
import com.lp.module_lib.base.BaseARouter;
import com.lp.module_lib.base.BaseActivity;
import com.lp.module_lib.utils.AppManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MainActivity extends BaseActivity {
    @BindView(R.id.frameLayout)
    FragmentContainerView mFrameLayout;
    @BindView(R.id.iv_0)
    ImageView mIv0;
    @BindView(R.id.llt_0)
    LinearLayout mLlt0;
    @BindView(R.id.iv_1)
    ImageView mIv1;
    @BindView(R.id.llt_1)
    LinearLayout mLlt1;
    @BindView(R.id.llt_fa)
    LinearLayout mLltFa;
    @BindView(R.id.iv_2)
    ImageView mIv2;
    @BindView(R.id.llt_2)
    LinearLayout mLlt2;
    @BindView(R.id.iv_3)
    ImageView mIv3;
    @BindView(R.id.llt_3)
    LinearLayout mLlt3;
    @BindView(R.id.iv_fa)
    ImageView mIvFa;

    private Fragment[] mFragments;
    private int index = 0, currentTabIndex = -1;
    private long exitTime = 0;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.app_activity_main;
    }

    @Override
    protected void initData() {

        Fragment HomeFragment = (Fragment) ARouter.getInstance().build(BaseARouter.RouterHome).navigation();
        Fragment VideoFragment = (Fragment) ARouter.getInstance().build(BaseARouter.RouterJhxw).navigation();
        Fragment NewsFragment = (Fragment) ARouter.getInstance().build(BaseARouter.RouterNews).navigation();
        Fragment MineFragment = (Fragment) ARouter.getInstance().build(BaseARouter.RouterHome).navigation();

        mFragments = new Fragment[]{
                HomeFragment,
                VideoFragment,
                NewsFragment,
                MineFragment};
        onTextClicked();
    }

    @OnClick({R.id.llt_0, R.id.llt_1, R.id.llt_2, R.id.llt_3, R.id.iv_fa})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.llt_0:
                onClick0();
                break;
            case R.id.llt_1:
                onClick1();
                break;
            case R.id.llt_2:
                onClick2();
                break;
            case R.id.llt_3:
                onClick3();
                break;
            case R.id.iv_fa:
//                ARouter.getInstance().build(BaseARouter.RouterZhjySend).navigation();
                break;
        }
    }

    public void onClick0() {
        index = 0;
        mIv0.setImageResource(R.mipmap.main_ji_check);
        if (currentTabIndex != index) {
            changeColor();
        }
        onTextClicked();
    }

    public void onClick1() {
        index = 1;
        mIv1.setImageResource(R.mipmap.main_video_check);
        if (currentTabIndex != index) {
            changeColor();
        }
        onTextClicked();
    }

    public void onClick2() {
        index = 2;
        mIv2.setImageResource(R.mipmap.main_xing_check);
        if (currentTabIndex != index) {
            changeColor();
        }
        onTextClicked();
    }

    public void onClick3() {
        index = 3;
        mIv3.setImageResource(R.mipmap.main_mine_check);
        if (currentTabIndex != index) {
            changeColor();
        }
        onTextClicked();
    }


    private void onTextClicked() {
        FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
        if (currentTabIndex != index) {
            if (currentTabIndex != -1) {
                trx.hide(mFragments[currentTabIndex]);
            } else {
                mIv0.setImageResource(R.mipmap.main_ji_check);
            }

            if (!mFragments[index].isAdded()) {
                trx.add(R.id.frameLayout, mFragments[index]);
            }
            trx.show(mFragments[index]).commit();
            currentTabIndex = index;
        }
    }

    public void changeColor() {
        switch (currentTabIndex) {
            case 0:
                mIv0.setImageResource(R.mipmap.main_ji_uncheck);
                break;
            case 1:
                mIv1.setImageResource(R.mipmap.main_video_uncheck);
                break;
            case 2:
                mIv2.setImageResource(R.mipmap.main_xing_uncheck);
                break;
            case 3:
                mIv3.setImageResource(R.mipmap.main_mine_uncheck);
                break;
            default:
                break;
        }
    }

    //防止出现乱屏
    @SuppressLint("MissingSuperCall")
    @Override
    protected void onSaveInstanceState(Bundle outState) {

    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                showToast(getString(R.string.app_press_the_exit_system_again));
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
                AppManager.getAppManager().AppExit(this);
            }
            return false;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }
}